package com.ibigcat.module.mdm.service.impl;

import com.ibigcat.module.mdm.service.CodeRuleSetService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.ibigcat.module.mdm.controller.admin.coderuleset.vo.*;
import com.ibigcat.module.mdm.dao.entity.CodeRuleSetDO;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.util.object.BeanUtils;

import com.ibigcat.module.mdm.dao.mapper.CodeRuleSetMapper;

import static com.ibigcat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ibigcat.module.mdm.enums.ErrorCodeConstants.*;

/**
 * 编码规则配置明细 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CodeRuleSetServiceImpl implements CodeRuleSetService {

    @Resource
    private CodeRuleSetMapper codeRuleSetMapper;

    @Override
    public Long createCodeRuleSet(CodeRuleSetSaveReqVO createReqVO) {
        // 插入
        CodeRuleSetDO codeRuleSet = BeanUtils.toBean(createReqVO, CodeRuleSetDO.class);
        codeRuleSetMapper.insert(codeRuleSet);
        // 返回
        return codeRuleSet.getId();
    }

    @Override
    public void updateCodeRuleSet(CodeRuleSetSaveReqVO updateReqVO) {
        // 校验存在
        validateCodeRuleSetExists(updateReqVO.getId());
        // 更新
        CodeRuleSetDO updateObj = BeanUtils.toBean(updateReqVO, CodeRuleSetDO.class);
        codeRuleSetMapper.updateById(updateObj);
    }

    @Override
    public void deleteCodeRuleSet(Long id) {
        // 校验存在
        validateCodeRuleSetExists(id);
        // 删除
        codeRuleSetMapper.deleteById(id);
    }

    private void validateCodeRuleSetExists(Long id) {
        if (codeRuleSetMapper.selectById(id) == null) {
            throw exception(CODE_RULE_SET_NOT_EXISTS);
        }
    }

    @Override
    public CodeRuleSetDO getCodeRuleSet(Long id) {
        return codeRuleSetMapper.selectById(id);
    }

    @Override
    public PageResult<CodeRuleSetDO> getCodeRuleSetPage(CodeRuleSetPageReqVO pageReqVO) {
        return codeRuleSetMapper.selectPage(pageReqVO);
    }

}